#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    :   4cilent.py
@Time    :   2020/06/25 02:43:45
@Author  :   Malygos_l 
@Version :   1.0
@Contact :   751756061@qq.com
@WebSite :   www.gitee.com/malygos_l
'''
# Start typing your code from here
'''
多人聊天
'''
import socket
import threading
import os
import get

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = (get.get_ip(), 8888)
s.connect(addr)


def recv_msg():
    print("连接成功！现在可以接收消息！\n")
    while True:
        try:
            response = s.recv(1024)
            print(response.decode("gbk"))
        except ConnectionResetError:
            print("服务器关闭，聊天已结束！")
            s.close()
            break
    os._exit(0)


def send_msg():
    print("连接成功！现在可以发送消息！\n")
    # print("输入消息按回车来发送")
    print("输入escape来退出聊天")
    while True:
        msg = input()
        if msg == "escape":
            print("你退出了聊天")
            s.close()
            break
        s.send(msg.encode("gbk"))
    os._exit(0)


threads = [
    threading.Thread(target=recv_msg),
    threading.Thread(target=send_msg)
]
for t in threads:
    t.start()